/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean.TX.CustomerIOTRPortal;

import Web_Helper.Helper_PaymentValidator;
import Web_Helper.pointPurchaseItem;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import session.TX.ULPSBRemote;

/**
 *
 * @author Randall Chia
 */
@ManagedBean(name = "CustomerPointsMB")
@SessionScoped
public class CustomerPointsMB {
    
    private List<pointPurchaseItem> purchaseList;    
    private pointPurchaseItem selectedPurchaseItem;
    private double purchasePrice;
    private Date transactionTimestamp;
    private String purchaseStatusMSG;
    
    
    @EJB
    private ULPSBRemote ulpRemote;

    /** Creates a new instance of CustomerPointsMB */
    public CustomerPointsMB() {
        purchaseList = new ArrayList<pointPurchaseItem>();
        purchaseList.add(new pointPurchaseItem(1000, 40.0));
        purchaseList.add(new pointPurchaseItem(5000, 180.0));
        purchaseList.add(new pointPurchaseItem(10000, 320.0));
    }
    
    
    public void updatePurchasePrice() {
        purchasePrice = selectedPurchaseItem.getPrice();
    }
    
    
    
    
    public void validatePayment() {

        boolean validationResult = false;
        try {
            validationResult = Helper_PaymentValidator.validatePayment();
        } catch (IOException e) {
        }

        if (validationResult == true) {
            System.out.println("Payment Validation: SUCCESS");

            setTransactionTimestamp(new Date());

            String customer = FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("email").toString();
            Long userID = (Long)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("accountId");

            ulpRemote.awardPoints(userID, getSelectedPurchaseItem().getQuantity());
            System.out.println("Gave " + getSelectedPurchaseItem().getQuantity() + " points to " + customer);

            //create purchase log entry
            ulpRemote.createPointPurchaseEntry_Users(getTransactionTimestamp(), customer, getSelectedPurchaseItem().getPrice());
            
            
            purchaseStatusMSG = "<h4>LOYALTY POINTS PURCHASED</h4>"
            + "<h4>Timestamp: </h4>" + transactionTimestamp + "<br/>"
            + "<h4>Buyer: </h4>" + customer + "<br/>"
            + "<h4>Points Purchased: </h4>" + selectedPurchaseItem.getQuantity()
            ;
            
            selectedPurchaseItem = null;
            
        }//end THEN block
        else {
            System.out.println("Payment Validation: FAIL");
        }

    }

    /**
     * @return the purchaseList
     */
    public List<pointPurchaseItem> getPurchaseList() {
        return purchaseList;
    }

    /**
     * @param purchaseList the purchaseList to set
     */
    public void setPurchaseList(List<pointPurchaseItem> purchaseList) {
        this.purchaseList = purchaseList;
    }

    /**
     * @return the selectedPurchaseItem
     */
    public pointPurchaseItem getSelectedPurchaseItem() {
        return selectedPurchaseItem;
    }

    /**
     * @param selectedPurchaseItem the selectedPurchaseItem to set
     */
    public void setSelectedPurchaseItem(pointPurchaseItem selectedPurchaseItem) {
        this.selectedPurchaseItem = selectedPurchaseItem;
    }

    /**
     * @return the purchasePrice
     */
    public double getPurchasePrice() {
        return purchasePrice;
    }

    /**
     * @param purchasePrice the purchasePrice to set
     */
    public void setPurchasePrice(double purchasePrice) {
        this.purchasePrice = purchasePrice;
    }

    /**
     * @return the transactionTimestamp
     */
    public Date getTransactionTimestamp() {
        return transactionTimestamp;
    }

    /**
     * @param transactionTimestamp the transactionTimestamp to set
     */
    public void setTransactionTimestamp(Date transactionTimestamp) {
        this.transactionTimestamp = transactionTimestamp;
    }
    
    
    
    
    
    
    
    
    
    
    
    /**
     * @return the PurchaseStatusMSG
     */
    public String getPurchaseStatusMSG() {
        return purchaseStatusMSG;
    }

    /**
     * @param PurchaseStatusMSG the PurchaseStatusMSG to set
     */
    public void setPurchaseStatusMSG(String purchaseStatusMSG) {
        this.purchaseStatusMSG = purchaseStatusMSG;
    }
    
    
    
    
}
